//
//  SaleViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/27.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "SaleViewController.h"
#import "NomalPullLayout.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "ImageColl.h"
#import "SnowTableViewCell.h"
#import "AFNetworking.h"
#import "womenDetailViewController.h"

@interface SaleViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, assign) CGFloat offHeigt;
@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, retain) AFHTTPRequestOperationManager *man;

@end

@implementation SaleViewController

- (void)dealloc
{
    [_collection release];
    [_table release];
    [_array release];
    [_man release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self beginConnection];
    self.offHeigt = [[UIScreen mainScreen]bounds].size.height / 667;
    self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
    [self createTableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
    // Do any additional setup after loading the view.
}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)beginConnection
{

    [AsyncConnection asyncConnection:THREE_SALE parmaters:nil block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [[dic objectForKey:@"datas"]objectForKey:@"SpecialList"];
        [self.table reloadData];
        
    }path:@"main_sale"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}


- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
//    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.table release];
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210 * _offHeigt;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SnowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[[SnowTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"]autorelease];
    
        
    }
    cell.title.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"MainTitle"];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[[self.array objectAtIndex:indexPath.row]objectForKey:@"Picture"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    cell.subTitle.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"SubTitle"];
    cell.subTitle.font = [UIFont systemFontOfSize:15];
    cell.subTitle.textColor = [UIColor lightGrayColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    womenDetailViewController *web = [[womenDetailViewController alloc]init];
    NSString *str = [NSString stringWithFormat:@"%@%@%@", @"http://freshapp.cn/S", [[self.array objectAtIndex:indexPath.row]objectForKey:@"Id"],@".htm"];
    web.url = [NSURL URLWithString:str];
    [self.navigationController pushViewController:web animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
